'use strict';

const BaseModel = require('./BaseModel');

/**
 * 地区 表信息
 */
class SysAreaModel extends BaseModel {

  constructor() {
    super('sys_area', 'code');
    
    this.entity = class entity {
      constructor() {
        this.code = null;
        this.name = null;
        this.sortIndex = null;
        this.parentCode = null;
        this.remarks = null;
        this.creatorId = null;
        this.createTime = null;
        this.updateTime = null;
        this.type = null;
      }
    };
  }
  /**
   * 添加单位
   */
  async addArea(app, models) {
    return await app.mysql.insert('sys_area', models);
  }
  /**
   * 删除单位
   */
  async delArea(app, code) {
    return await app.mysql.delete('sys_area', {
      code,
    });
  }
  /**
   * 修改单位
   */
  async updateArea(app, code, models) {
    return app.mysql.update('sys_area', models, {
      where: {
        code,
      },
    });
  }
  /**
   * 查询所有单位
   */
  async findAllArea(app, request) {
    const sqls = [];
    sqls.push(`select * from ${this.tableName} where 1=1 `);
    // 用于判断用户权限
    if (request.userRights && request.userRights.areaCodes) {
      sqls.push(`and code in (${request.userRights.areaCodes}) `);
    }
    const sql = sqls.join(' ');
    app.logger.debug('SQL：', sql);
    return await app.mysql.query(sql);
  }

  /**
   * 查询单位详情
   */
  async findAreaByCode(app, code) {
    const sql = `select * from sys_area where code = "${code}"`;
    console.log(63, sql);
    return await app.mysql.query(sql);
  }
}

module.exports = SysAreaModel;
